<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Http\Requests\ProductCreateRequest;
use App\Http\Requests\ProductSearchRequest;
use App\Models\Event;
use App\Models\PriceHistory;
use App\Models\Product;
use App\Utils\HelperUtil;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ProductController extends Controller
{
    public function store(ProductCreateRequest $request)
    {
        return DB::transaction(function () use ($request) {
            $exists = Product::where('name', $request->name)->exists();

            if ($exists) {
                return HelperUtil::responseError('名称已经存在');
            }

            $data = $request->all();

            if (empty($data['sort'])) {
                $data['sort'] = 1000000000;
            }

            $product = Product::create($data);

            Event::addEvent($product->id, Event::PRODUCT_CREATE, $request->all());

            return HelperUtil::responseSuccess(null);
        });
    }

    public function show(Product $product)
    {
        return HelperUtil::responseSuccess($product);
    }

    public function update(Product $product, ProductCreateRequest $request)
    {
        return DB::transaction(function () use ($product, $request) {

            PriceHistory::addHistoryByProduct($product);

            $product->update($request->all());

            $content = ['product' => $product->toArray(), 'request' => $request->all()];

            Event::addEvent($product->id, Event::PRODUCT_UPDATE, $content);

            return HelperUtil::responseSuccess(null);
        });
    }

    public function destroy(Product $product)
    {
        return DB::transaction(function () use ($product) {
            $product->update(['deleted' => 1]);

            Event::addEvent($product->id, Event::PRODUCT_DEL, "");

            return HelperUtil::responseSuccess(null);
        });
    }

    public function getList(ProductSearchRequest $request)
    {
        $query = Product::with(['brand', 'category']);

        $query->where('deleted', 0);

        // order by
        if (!empty($request->prop) && !empty($request->order)) {
            $order = 'desc';

            if ($request->order == HelperUtil::ORDER_ASC) {
                $order = 'asc';
            }

            $query->orderBy($request->prop, $order);
        } else {
            $query->orderByDesc('sort');
            $query->orderByDesc('id');
        }

        if (!empty($request->name)) {
            $query->where('name', 'like', '%' . $request->name . '%');
        }

        if ($request->brand_id > 0) {
            $query->where(['brand_id' => $request->brand_id]);
        }

        $data = $query->paginate($request->limit);

        return HelperUtil::responseSuccess($data);
    }

    public function querySearch(Request $request)
    {
        $keyword = $request->get('keyword');

        $data = [];

        if (!empty($keyword)) {
            $data = Product::where('name', 'like', '%' . $keyword . '%')
                ->limit(20)
                ->get();
        }

        return HelperUtil::responseSuccess($data);
    }
}
